package com.ddxz.middleground.master.service.impl;

import com.ddxz.middleground.master.dto.MasterHeader;
import com.ddxz.middleground.master.dto.permission.Permission;
import com.ddxz.middleground.master.dto.permission.PermissionDetail;
import com.ddxz.middleground.master.dto.permission.PermissionInfo;
import com.ddxz.middleground.master.dto.permission.PermissionPage;
import com.ddxz.middleground.master.entity.PermissionEntity;
import com.ddxz.middleground.master.mapper.MenuPermissionMapper;
import com.ddxz.middleground.master.mapper.PermissionMapper;
import com.ddxz.middleground.master.mapper.RolePermissionMapper;
import com.ddxz.middleground.master.service.PermissionService;
import com.ddxz.tool.core.common.CommonUtil;
import com.ddxz.tool.core.common.DdxzTree;
import com.ddxz.tool.core.common.DdxzTreeUtil;
import com.ddxz.tool.core.convert.BeanUtil;
import com.ddxz.tool.core.convert.CollectionUtil;
import com.ddxz.tool.core.convert.DdxzUtil;
import com.ddxz.tool.core.convert.StringUtil;
import com.ddxz.tool.web.dto.DdxzResponse;
import com.ddxz.tool.web.dto.PageResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.ddxz.middleground.master.util.ErrorStatus.*;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/8/3
 */

@Service
public class PermissionServiceImpl implements PermissionService, DdxzTreeUtil {
    @Override
    public DdxzResponse permissionList(MasterHeader header, PermissionPage page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<PermissionEntity> entityPage = permissionMapper.permissionSearch(page.getStatus(), page.getKeyword(), header.getTenant());
        return DdxzResponse.correct(PageResponse.getInstance(entityPage, Permission.class, DdxzUtil.SECOND_DATE));
    }

    @Override
    public DdxzResponse permissionDetail(MasterHeader header, String permissionId) {
        PermissionEntity entity = permissionMapper.permissionDetail(permissionId, header.getTenant());
        CommonUtil.ddxzAssetNull(entity, PERMISSION_NOT_EXISTS);
        return DdxzResponse.correct(BeanUtil.getTarget(entity, PermissionDetail.class, DdxzUtil.SECOND_DATETIME));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DdxzResponse permissionSave(MasterHeader header, PermissionInfo info) {
        PermissionEntity entity = BeanUtil.getTarget(info, PermissionEntity.class);
        if(!StringUtil.isEmpty(info.getId())) {
            permissionMapper.permissionModify(entity, header.getTenant());
            return DdxzResponse.correct("权限保存成功", entity.getId());
        }

        Integer num = permissionMapper.getNumByName(info.getName(), header.getTenant());
        CommonUtil.ddxzAssetGreat(num, 0, PERMISSION_NAME_REPEAT);
        num = permissionMapper.getNumByCode(info.getCode(), header.getTenant());
        CommonUtil.ddxzAssetGreat(num, 0, PERMISSION_CODE_REPEAT);

        entity.setId(CommonUtil.generatorId("ddxz_permission"));
        entity.setUserId(header.getUserId());
        entity.setCreator(header.getNickname());
        permissionMapper.permissionSave(entity, header.getTenant());
        return DdxzResponse.correct("权限保存成功", entity.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DdxzResponse permissionRemove(MasterHeader header, String permissionId) {
        Integer num = roleMapper.getNumByPermissionId(permissionId, header.getTenant());
        CommonUtil.ddxzAssetGreat(num, 0, PERMISSION_IN_USE);
        permissionMapper.removePermission(permissionId, header.getTenant());
        menuMapper.remove(permissionId, header.getTenant());
        return DdxzResponse.correct(OK_MESSAGE);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DdxzResponse permissionGrant(MasterHeader header, String permissionId, List<DdxzTree> menuTree) {
        if(CollectionUtil.isEmpty(menuTree)) {
            return DdxzResponse.correct("绑定成功", OK_MESSAGE);
        }

        menuTree.forEach(tree -> {
            menuMapper.mergerGrant(tree.getId(), tree.getState(), permissionId, header.getTenant());
            permissionGrant(header, permissionId, tree.getChildren());
        });

        return DdxzResponse.correct("绑定成功", OK_MESSAGE);
    }

    private final PermissionMapper permissionMapper;
    private final RolePermissionMapper roleMapper;
    private final MenuPermissionMapper menuMapper;

    public PermissionServiceImpl(PermissionMapper permissionMapper, RolePermissionMapper roleMapper, MenuPermissionMapper menuMapper) {
        this.permissionMapper = permissionMapper;
        this.roleMapper = roleMapper;
        this.menuMapper = menuMapper;
    }
}
